//! 核心引擎模块测试
//! 
//! 测试 src/core.rs 中的 CoreEngine 功能

use dataforge::{CoreEngine, GenConfig, GenerationStrategy, Language};

#[test]
fn test_core_engine_creation() {
    let config = GenConfig {
        batch_size: 100,
        locale: Language::ZhCN,
        null_probability: 0.1,
        strategy: GenerationStrategy::Random,
        parallelism: 2,
    };
    
    let engine = CoreEngine::new(config);
    assert_eq!(engine.config().batch_size, 100);
    assert_eq!(engine.config().parallelism, 2);
}

#[test]
fn test_core_engine_batch_generation() {
    let config = GenConfig {
        batch_size: 100,
        locale: Language::ZhCN,
        null_probability: 0.1,
        strategy: GenerationStrategy::Random,
        parallelism: 2,
    };
    
    let engine = CoreEngine::new(config);
    
    // 测试批量生成
    let result = engine.generate_batch(50);
    assert!(result.is_ok());
    
    let data = result.unwrap();
    assert_eq!(data.len(), 50);
}

#[test]
fn test_core_engine_metrics() {
    let config = GenConfig {
        batch_size: 100,
        locale: Language::ZhCN,
        null_probability: 0.1,
        strategy: GenerationStrategy::Random,
        parallelism: 2,
    };
    
    let engine = CoreEngine::new(config);
    
    // 生成一些数据
    let _ = engine.generate_batch(50);
    
    // 验证指标
    let metrics = engine.metrics();
    assert_eq!(metrics.generated_count(), 50);
    assert_eq!(metrics.error_count(), 0);
}

#[test]
fn test_core_engine_prometheus_export() {
    let config = GenConfig {
        batch_size: 100,
        locale: Language::ZhCN,
        null_probability: 0.1,
        strategy: GenerationStrategy::Random,
        parallelism: 2,
    };
    
    let engine = CoreEngine::new(config);
    let _ = engine.generate_batch(50);
    
    // 测试Prometheus导出
    let metrics = engine.metrics();
    let prometheus_output = metrics.export_prometheus();
    assert!(prometheus_output.contains("dataforge_generated_total 50"));
}

#[test]
fn test_core_engine_different_strategies() {
    let strategies = vec![
        GenerationStrategy::Random,
        GenerationStrategy::Sequential,
        GenerationStrategy::Mixed,
    ];
    
    for strategy in strategies {
        let config = GenConfig {
            batch_size: 10,
            locale: Language::ZhCN,
            null_probability: 0.0,
            strategy,
            parallelism: 1,
        };
        
        let engine = CoreEngine::new(config);
        let result = engine.generate_batch(5);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().len(), 5);
    }
}

#[test]
fn test_core_engine_null_probability() {
    let config = GenConfig {
        batch_size: 100,
        locale: Language::ZhCN,
        null_probability: 1.0, // 100% null
        strategy: GenerationStrategy::Random,
        parallelism: 1,
    };
    
    let engine = CoreEngine::new(config);
    let result = engine.generate_batch(10);
    assert!(result.is_ok());
    
    let data = result.unwrap();
    // 验证大部分数据为null（由于随机性，不要求100%）
    let null_count = data.iter().filter(|v| v.is_null()).count();
    assert!(null_count >= 7); // 至少70%为null
}